package com.trace.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.Role;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import com.trace.app.entity.vo.LabelValueVo;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.mapper.RoleMapper;
import com.trace.app.service.RoleService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色表(Role)表服务实现类
 *
 * @author lionkliu
 * @since 2024-03-31 09:54:48
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        Page<Role> page = page(new Page<>(pageNum, pageSize), queryWrapper);
        PageVo<Role> pageVo = new PageVo<>();
        pageVo.setResult(page.getRecords());
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(Role role) {
        return save(role) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Role role) {
        LambdaUpdateWrapper<Role> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(Role::getId, role.getId())
                .set(Role::getName, role.getName())
                .set(Role::getKey, role.getKey())
                .set(Role::getRemark, role.getKey());

        update(null, updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getLabelValue() {
        List<LabelValueVo> collect = list().stream().map((category -> {
            LabelValueVo labelValueVo = new LabelValueVo();
            labelValueVo.setValue(String.valueOf(category.getId()));
            labelValueVo.setLabel(category.getName());
            return labelValueVo;
        })).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }
}

